package com.tanhua.model.vo;

import com.tanhua.model.mongo.Option;
import com.tanhua.model.mongo.Question;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author huangyuahan
 * @version 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class QuestionVo implements Serializable {
    //题目id
    private String id;
    //题目
    private String question;
    //
    private List<OptionsVo> options;


    // 绑定题目和list<选项>并把两者id转化为string类型
    public QuestionVo init(Question question,List<Option> options){
        QuestionVo questionVo = new QuestionVo();
        BeanUtils.copyProperties(question,questionVo);
        questionVo.setId(question.getId().toHexString());
        List<OptionsVo> vos = new ArrayList<>();
        //问题id -> String
        for (Option option : options) {
            if (option.getQuestionId().equals(question.getId())){
                OptionsVo optionsVo= new OptionsVo();
                BeanUtils.copyProperties(option,optionsVo);
                optionsVo.setId(option.getId().toHexString());
                vos.add(optionsVo);
            }
        }
        questionVo.setOptions(vos);
        return questionVo;
    }

}
